package com.ygxsk.carhome.utils;

import android.text.TextUtils;

import com.ygxsk.carhome.R;

import org.apache.commons.lang3.StringUtils;

public class DesensitizedUtil {

    /**
     * 姓名脱敏
     * 一位时不脱敏，两位时‘姓’脱敏，两位以上保留首尾中间脱敏
     *
     * @param str
     * @return
     */
    public static String nameDesensitization(String str) {
        if (StringUtils.isEmpty(str)) {
            // if (str == null || str.equals("")){
            return str;
        }
        char[] sArr = str.toCharArray();
        if (sArr.length == 2) {
            return "*" + sArr[1];
        } else if (sArr.length > 2) {
            for (int i = 1; i < sArr.length - 1; i++) {
                // if ('·' != sArr[i]) {
                sArr[i] = '*';
                // }
            }
            return new String(sArr);
        }
        return str;
    }


    /**
     * @param fullName 输入姓名
     * @return null
     * @description 姓名脱敏
     */
    public static String desensitizedName(String fullName) {
        if (!TextUtils.isEmpty(fullName)) {
            String name = StringUtils.left(fullName, 1);
            return StringUtils.rightPad(name, StringUtils.length(fullName), "*");
        }
        return fullName;
    }

    /**
     * @param phoneNumber 手机号
     * @return null
     * @description 手机号脱敏
     */
    public static String desensitizedPhoneNumber(String phoneNumber) {

        String firstCode = com.blankj.utilcode.util.StringUtils.getString(R.string.phonestar);

        //String result = "(\w{3})\w*(\w{4})";
        //String result = "(\w{3})\w*(\w{4})";

        if (!TextUtils.isEmpty(phoneNumber)) {
            phoneNumber = phoneNumber.replaceAll(firstCode, "$1****$2");
        }

        return phoneNumber;
    }

    /**
     * @description 身份证号脱敏
     * @param idNumber 身份证号码
     * @return null
     */
//    public static String desensitizedIdNumber(String idNumber){
//        if (!TextUtils.isEmpty(idNumber)) {
//            if (idNumber.length() == 15){
//                idNumber = idNumber.replaceAll("(\w{6})\w*(\w{3})", "$1******$2");
//            }
//            if (idNumber.length() == 18){
//                idNumber = idNumber.replaceAll("(\w{6})\w*(\w{3})", "$1*********$2");
//            }
//        }
//        return idNumber;
//    }

    /**
     * @param address 地址
     * @return null
     * @description 地址脱敏
     */
    public static String desensitizedAddress(String address) {
        if (!TextUtils.isEmpty(address)) {
            return StringUtils.left(address,
                    3).concat(StringUtils.removeStart(StringUtils.leftPad(StringUtils.right(address,
                    address.length() - 11), StringUtils.length(address), "*"), "***"));
        }
        return address;
    }

}
